//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include "reportwindow.h"
#include "utils.h"


enum cIDS{ cBtnIDSend=12500, cBtnIDCopy };

BEGIN_EVENT_TABLE(CReportWindow, wxDialog)
	EVT_BUTTON(cBtnIDSend, CReportWindow::OnSend)
	EVT_UPDATE_UI(cBtnIDSend, CReportWindow::OnSendUI)
END_EVENT_TABLE()


CReportWindow::CReportWindow(wxWindow* pParent, const wxString& strUserName):
	wxDialog( pParent, wxID_ANY,_("My router is compatible with Router Commander"), wxDefaultPosition, wxDefaultSize,
	wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER|wxSTAY_ON_TOP)
{
	wxBoxSizer* szMain = new wxBoxSizer(wxVERTICAL);	

	wxStaticText* plbNote = new wxStaticText(this, -1, _("To improve quality of Router Commander, please, send this report to developer.\nYour report will help to better support your router in the future releases."));
	szMain->Add(plbNote,0,wxALL|wxALIGN_CENTER, 10);

	wxStaticText* plbNote0 = new wxStaticText(this, -1, _("Found problems, compatibility issues:"));
	szMain->Add(plbNote0,0,wxALL|wxALIGN_CENTER, 10);

	m_pedtLog = new wxTextCtrl(this, -1, wxT("No problems found"), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
	szMain->Add(m_pedtLog,1,wxEXPAND|wxALL|wxALIGN_CENTER, 10);

	//Form
	wxFlexGridSizer* fgs = new wxFlexGridSizer(6, 2, 10, 5);

	wxStaticText* plbManuf = new wxStaticText(this, -1, _("Router manufacturer:"));
	wxStaticText* plbModel = new wxStaticText(this, -1, _("Router model:"));

	m_pedtRouterManufacturer= new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize);
	m_pedtRouterModel= new wxTextCtrl(this, -1, wxT(""), wxDefaultPosition, wxDefaultSize);

	fgs->Add(plbManuf);
	fgs->Add(m_pedtRouterManufacturer);

	fgs->Add(plbModel);
	fgs->Add(m_pedtRouterModel);

	{
		wxStaticText* pText = new wxStaticText(this, -1, _("Built-in Wireless?"));
		m_pWireless = new wxCheckBox(this, -1, wxT(""));
		
		fgs->Add(pText);
		fgs->Add(m_pWireless);
	}

	{
		wxStaticText* pText = new wxStaticText(this, -1, _("Built-in ADSL Modem?"));
		m_pADSL = new wxCheckBox(this, -1, wxT(""));
		
		fgs->Add(pText);
		fgs->Add(m_pADSL);
	}

	{
		wxStaticText* pText = new wxStaticText(this, -1, _("*Default user name:"));
		m_pedtDefUserName = new wxTextCtrl(this, -1, strUserName);
		
		fgs->Add(pText);
		fgs->Add(m_pedtDefUserName);
	}

	{
		wxStaticText* pText = new wxStaticText(this, -1, _("*Default password:"));
		m_pedtDefPassword = new wxTextCtrl(this, -1);
		
		fgs->Add(pText);
		fgs->Add(m_pedtDefPassword);
	}

	fgs->SetFlexibleDirection(wxHORIZONTAL);
	fgs->AddGrowableCol(1, 1);

	szMain->Add(fgs, 0, wxALIGN_LEFT|wxLEFT|wxEXPAND, 10 );

	wxStaticText* plbNote2 = new wxStaticText(this, -1, _("*Leave blanc, if you use non default user name or password"));
	szMain->Add(plbNote2,0,wxALL|wxALIGN_CENTER, 10);

		
	//Buttons
	wxSizer* sz = CreateStdDialogButtonSizer(wxCANCEL);

	wxButton* pSendLog = new wxButton(this, cBtnIDSend, _("Send report to developer") );
	sz->Add(pSendLog, 0, wxLEFT, 10);

	szMain->Add(sz,0, wxALIGN_CENTER|wxALL, 10);

	SetSizer(szMain);
	szMain->Fit(this);
}

CReportWindow::~CReportWindow(void)
{
}

void CReportWindow::OnSend(wxCommandEvent& event)
{
	wxString strURL = wxT("mailto:support@routercommander.com?subject=router compatibility.");

	strURL+=wxT(" Manufacturer:");
	strURL+=m_pedtRouterManufacturer->GetValue();

	strURL+=wxT(" Model:");
	strURL+=m_pedtRouterModel->GetValue();

	strURL+=wxT(" Wireless:");
	strURL+=m_pWireless->GetValue()?wxT("Built-in"):wxT("None");

	strURL+=wxT(" ADSL:");
	strURL+=m_pADSL->GetValue()?wxT("Built-in"):wxT("None");

	strURL+=wxT(" DefUser:");
	strURL+=m_pedtDefUserName->GetValue();

	strURL+=wxT(" DefPassw:");
	strURL+=m_pedtDefPassword->GetValue();

	strURL+=wxT("&body=");
	wxString strLog = m_pedtLog->GetValue();

	strURL+=URLEncode(strLog);
	wxLaunchDefaultBrowser(strURL);
}

void CReportWindow::OnSendUI(wxUpdateUIEvent& event)
{
	bool bValid = !m_pedtRouterModel->GetValue().empty() &&
		!m_pedtRouterManufacturer->GetValue().empty();

	event.Enable( bValid );
}